<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      @submit.native.prevent
    >
      <!-- <el-form-item label="表名" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="表描述" prop="comment">
        <!-- <el-input
          v-model="queryParams.comment"
          placeholder="请输入表名"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.tableName"
          clearable
          filterable
          remote
          :remote-method="getDatacomment"
          placeholder="请输入表名"
        >
          <el-option
            v-for="item in options"
            :key="item.tableName"
            :label="`${item.comment}`"
            :value="item.tableName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据库表名" prop="tableName">
        <el-select
          v-model="queryParams.tableName"
          clearable
          filterable
          remote
          :remote-method="getDatas"
          placeholder="请输入数据库表名"
        >
          <el-option
            v-for="item in options"
            :key="item.tableName"
            :label="`${item.tableName}`"
            :value="item.tableName"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.tableName"
          placeholder="请输入数据库表名"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <!-- <el-form-item label="列名" prop="columnName">
        <el-input
          v-model="queryParams.columnName"
          placeholder="请输入列名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="typeLength">
        <el-input
          v-model="queryParams.typeLength"
          placeholder="请输入长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否为空" prop="isNullable">
        <el-input
          v-model="queryParams.isNullable"
          placeholder="请输入是否为空"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否主键" prop="isPrimary">
        <el-input
          v-model="queryParams.isPrimary"
          placeholder="请输入是否主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否自增" prop="isAutoIncrement">
        <el-input
          v-model="queryParams.isAutoIncrement"
          placeholder="请输入是否自增"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注释" prop="comment">
        <el-input
          v-model="queryParams.comment"
          placeholder="请输入注释"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['conf:SysTableField:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['conf:SysTableField:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['conf:SysTableField:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['conf:SysTableField:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="trackingNumberHandle"
          >追加到字段列表</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="SysTableFieldList"
      @selection-change="handleSelectionChange"
      :height="600"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="表id" align="center" prop="tableId" /> -->
      <el-table-column label="列名" align="center" prop="columnName" />
      <el-table-column label="数据类型" align="center" prop="columnType" />
      <el-table-column label="长度" align="center" prop="typeLength" />
      <el-table-column label="是否为空" align="center" prop="isNullable">
        <template slot-scope="scope">
          <div>
            {{ scope.row.isNullable | state1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否主键" align="center" prop="isPrimary">
        <template slot-scope="scope">
          <div>
            {{ scope.row.isPrimary | state1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否自增" align="center" prop="isAutoIncrement">
        <template slot-scope="scope">
          <div>
            {{ scope.row.isAutoIncrement | state1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="注释" align="center" prop="comment" />
      <el-table-column label="操作类型" align="center" prop="actionType" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['conf:SysTableField:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['conf:SysTableField:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改表字段信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表id" prop="tableId">
          <el-input v-model="form.tableId" placeholder="请输入表id" />
        </el-form-item>
        <el-form-item label="列名" prop="columnName">
          <el-input v-model="form.columnName" placeholder="请输入列名" />
        </el-form-item>
        <el-form-item label="长度" prop="typeLength">
          <el-input v-model="form.typeLength" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="是否为空" prop="isNullable">
          <el-input v-model="form.isNullable" placeholder="请输入是否为空" />
        </el-form-item>
        <el-form-item label="是否主键" prop="isPrimary">
          <el-input v-model="form.isPrimary" placeholder="请输入是否主键" />
        </el-form-item>
        <el-form-item label="是否自增" prop="isAutoIncrement">
          <el-input
            v-model="form.isAutoIncrement"
            placeholder="请输入是否自增"
          />
        </el-form-item>
        <el-form-item label="注释" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入注释" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSysTableField,
  getSysTableField,
  delSysTableField,
  addSysTableField,
  updateSysTableField,
  selectTableFields,
  selectTableNameList,
} from "@/api/conf/SysTableField";

export default {
  name: "SysTableField",
  data() {
    return {
      options: [],
      list: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表字段信息表格数据
      SysTableFieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: null,
        tableId: null,
        columnName: null,
        columnType: null,
        typeLength: null,
        isNullable: null,
        isPrimary: null,
        isAutoIncrement: null,
        comment: null,
        actionType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tableId: [{ required: true, message: "表id不能为空", trigger: "blur" }],
        columnName: [
          { required: true, message: "列名不能为空", trigger: "blur" },
        ],
        columnType: [
          { required: true, message: "数据类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  filters: {
    state1(int) {
      if (int == 0) {
        return "是";
      }
      if (int == 1) {
        return "否";
      }
    },
  },
  created() {
    this.getList();
  },
  methods: {
    getDatacomment(key) {
      selectTableNameList({ comment: key }).then((response) => {
        this.options = response;
      });
    },
    getDatas(key) {
      selectTableNameList({ tableName: key }).then((response) => {
        this.options = response;
      });
    },
    trackingNumberHandle() {
      this.$emit("trackingNumberHandle", this.list);
    },
    /** 查询表字段信息列表 */
    getList() {
      this.loading = true;
      selectTableFields(this.queryParams).then((response) => {
        this.SysTableFieldList = response;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tableId: null,
        columnName: null,
        columnType: null,
        typeLength: null,
        isNullable: null,
        isPrimary: null,
        isAutoIncrement: null,
        comment: null,
        actionType: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.list = selection.map((item) => item);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加表字段信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSysTableField(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改表字段信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSysTableField(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysTableField(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除表字段信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSysTableField(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "conf/SysTableField/export",
        {
          ...this.queryParams,
        },
        `SysTableField_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
